package com.yunhe.sms.dao;

import com.yunhe.sms.control.UserController;
import com.yunhe.sms.mode.Teacher;
import com.yunhe.sms.mode.User;
import com.yunhe.sms.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    /**
     * 获取单个用户
     * @param userName
     * @param password
     * @param type
     * @return
     */
    public static User getOneUser(String userName, String password, Integer type){
        Connection conn = JdbcUtil.getConn();
        String sqlQuery = "select * from user where user_name = ? and password = ? and type = ?";
        PreparedStatement stmt = JdbcUtil.getstmt(conn, sqlQuery, userName, password, type);
        ResultSet resultSet = JdbcUtil.sqlQuery(stmt);
        User user = null;
        if (null!= resultSet){
            try {
                while (resultSet.next()){
                    String no = resultSet.getString("no");
                    String userName1 = resultSet.getString("user_name");
                    String pwd = resultSet.getString("password");
                    String realName = resultSet.getString("real_name");
                    int t = resultSet.getInt("type");
                    Integer otherId = resultSet.getInt("other_id");
                    String s = UserController.intTypeto2Str(t);
                    Teacher teacher = null;
                    if (null!= otherId){
                    }
                    user = new User(no,userName1,pwd,realName,s,teacher);
                    break;
                }
            } catch (SQLException e) {
            e.printStackTrace();
            }
        }
        JdbcUtil.close(conn,stmt,resultSet);
        return user;
    }

    /**
     * 获取所有用户
     * @return
     */
    public static List<User> getAllUser(){
        Connection conn = JdbcUtil.getConn();
        String sqlQuery = "select * from user order by type desc, no asc ";
        PreparedStatement stmt = JdbcUtil.getstmt(conn, sqlQuery);
        ResultSet resultSet = JdbcUtil.sqlQuery(stmt);
        User user = null;
        List<User> userList = new ArrayList<>();
        if (null!= resultSet){
            try {
                while (resultSet.next()){
                    String no = resultSet.getString("no");
                    String userName1 = resultSet.getString("user_name");
                    String pwd = resultSet.getString("password");
                    String realName = resultSet.getString("real_name");
                    int t = resultSet.getInt("type");
                    Integer otherId = resultSet.getInt("other_id");
                    String s = UserController.intTypeto2Str(t);
                    Teacher teacher = null;
                    if (null!= otherId){
                    }
                    user = new User(no,userName1,pwd,realName,s,teacher);
                    userList.add(user);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        JdbcUtil.close(conn,stmt,resultSet);
        return userList;
    }
    /**
     * 添加用户
     * @param user
     */
    public static void userInsert(User user){
        if (null!=user){
            int i = UserController.strTypeToInt(user.getType());
            Connection conn = JdbcUtil.getConn();
            String sqlInsert = " insert into user(no,user_name,password,real_name,type,other_id) values(?,?,?,?,?,?) ";
            Teacher teacher = user.getTeacher();
            int no = 0;
            if (null!=teacher){
                no = teacher.getId();
            }
            PreparedStatement stmt = JdbcUtil.getstmt(conn, sqlInsert, user.getId(), user.getName(), user.getPassword(),user.getRealName(),i,no);
            JdbcUtil.sqlUpdate(stmt);
            JdbcUtil.close(stmt);
            JdbcUtil.close(conn);
        }
    }

    /**
     * 修改用户
     * @param user
     */
    public static void userUpdate(User user){
        if (null!=user){
            int i = UserController.strTypeToInt(user.getType());
            Connection conn = JdbcUtil.getConn();
            String sqlInsert = " update user set user_name =?, password =?, real_name =?, type =?, other_id =? where no =? ";
            Teacher teacher = user.getTeacher();
            int no = 0;
            if (null!=teacher){
                no = teacher.getId();
            }
            PreparedStatement stmt = JdbcUtil.getstmt(conn, sqlInsert, user.getName(), user.getPassword(),user.getRealName(),i,no, user.getId());
            JdbcUtil.sqlUpdate(stmt);
            JdbcUtil.close(stmt);
            JdbcUtil.close(conn);
        }

    }

    /**
     * 删除用户
     * @param user
     */
    public static void userDle(User user){
        Connection conn = JdbcUtil.getConn();
        String sqlDle = " delete from user where user_name = ? and type = ?";
        String name = user.getName();
        PreparedStatement stmt = JdbcUtil.getstmt(conn, sqlDle, name,UserController.strTypeToInt(user.getType()));
        if(null!=stmt){
            JdbcUtil.sqlUpdate(stmt);
            JdbcUtil.close(stmt);
            JdbcUtil.close(conn);
        }
    }

}
